package gui.showrecipe;

import gui.addrecipe.Stars;
import gui.overview.OverviewPanel;
import gui.search.SearchPanel;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.GridBagLayout;
import javax.swing.JLabel;

import java.awt.GridBagConstraints;
import java.awt.Insets;
import org.jdom.JDOMException;

import start.Config;
import start.Start;

import xml.XMLRecipeReader;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.awt.Font;

/**
 * JPanel fuer den Inhalt des Frames zur Anzeige eines Rezepts
 * @author Dennis Wittkoetter
 *
 */
public class ShowRecipe_Content extends JPanel {
	/**ID zur Serialisierung*/
	private static final long serialVersionUID = 1L;
	/**XML-Reader zum Auslesen von Rezepten*/
	private static XMLRecipeReader xmlreader;
	
	//Layout-Objekte
	/**Textarea fuer Rezeptname*/
	private static JTextArea lblRecipeName;
	/**Textarea fuer Zutaten-Ueberschrift*/
	private static JTextArea lblIngredientsHeader;
	/**Textarea fuer Zutatenliste*/
	private static JTextArea lblIngredientsList;
	/**Textarea fuer Zubereitungsueberschrift*/
	private static JTextArea lblPreperationHeader;
	/**Textarea fuer Zutatentext*/
	private static JTextArea txtAreaPreperationText;
	/**Label fuer Bild*/
	private static JLabel lblPicture;
	/**Sterne fuer Bewertung*/
	private static Stars imgStars;
	/**Textarea fuer Personenzahl*/
	private static JTextArea lblPersAmount;
	/**Textarea fuer Kategorie*/
	private static JTextArea lblCategory;
	
	/**
	 * Konstruiert das Panel zur Anzeige der Informationen eines Rezepts
	 * @param selRow	Aktuell markierte Zeile in Ursprungstabelle
	 * @param table		ID der Ursprungstabelle
	 * @throws JDOMException
	 * @throws IOException
	 */
	public ShowRecipe_Content(int selRow, int table) throws JDOMException, IOException {
		xmlreader = new XMLRecipeReader(Start.getRezeptexml()); 
		
		//Layout-Formatierung
		int marginLeft = 10;
		int marginRight = 10;
		int titleTextSize = 24;
		int headerTextSize = 14;
		int plainTextSize = 13;
		
		//Variablen
		int recipeID = -1;
		String txt_lblRecipeName = "";
		String txt_lblIngredientsHeader = "";
		String txt_lblIngredientsList = "";
		String txt_lblPreperationHeader = "";
		String txt_lblPreperationText = "";
		String txt_lblPicture = "";
		String txt_lblPersAmount = "";
		String txt_lblCategories = "";
		
		if (selRow != -1) {
			if (table == 0)	//Bugfix
				recipeID = Integer.parseInt(OverviewPanel.getTable().getValueAt(selRow, 4).toString());
			else
				recipeID = Integer.parseInt(SearchPanel.getTable().getValueAt(selRow, 3).toString());
			
			txt_lblRecipeName = xmlreader.getName(recipeID);
			txt_lblIngredientsHeader = "Zutaten:";
			List<String>ingList = xmlreader.getIngredients(recipeID);
			for (int i=0; i<ingList.size(); i++) {
				txt_lblIngredientsList += ingList.get(i);
				if (i < ingList.size()-1)
					txt_lblIngredientsList +="\n";
			}
			
			txt_lblPreperationHeader = "Zubereitung:";
			txt_lblPreperationText = xmlreader.getPreperationTxt(recipeID);
			txt_lblPersAmount = "für "+xmlreader.getPersAmount(recipeID)+" Personen";
			txt_lblCategories = "Kategorien: "+xmlreader.getCategoryAll(recipeID);
			txt_lblPicture = xmlreader.getImageName(recipeID);
			//Bugfix
			if (!new File(Config.getBildpfad()+txt_lblPicture).exists())
				txt_lblPicture = "";
		}
		
		/** Layout einstellen */
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0};
		gridBagLayout.rowHeights = new int[]{10, 1, 15, 5, 5, 5, 20, 5, 50, 0};
		gridBagLayout.columnWeights = new double[]{1.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 5.0,0};
		setLayout(gridBagLayout);
		setBackground(Color.WHITE);
		
		/** Rezepttitel */
		lblRecipeName = new JTextArea(txt_lblRecipeName);
		lblRecipeName.setFont(new Font("Comic Sans MS", Font.BOLD, titleTextSize));
		lblRecipeName.setOpaque(false);
		lblRecipeName.setEditable(false);
		GridBagConstraints gbc_lblRecipeName = new GridBagConstraints();
		gbc_lblRecipeName.anchor = GridBagConstraints.NORTH;
		gbc_lblRecipeName.insets = new Insets(20, marginLeft, 10, marginRight);
		gbc_lblRecipeName.gridx = 0;
		gbc_lblRecipeName.gridy = 0;
		add(lblRecipeName, gbc_lblRecipeName);
		
		/** Zutaten */
		lblIngredientsHeader = new JTextArea(txt_lblIngredientsHeader);
		lblIngredientsHeader.setFont(new Font("Tahoma", Font.BOLD, headerTextSize));
		lblIngredientsHeader.setOpaque(false);
		lblIngredientsHeader.setEditable(false);
		GridBagConstraints gbc_lblIngredientsHeader = new GridBagConstraints();
		gbc_lblIngredientsHeader.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblIngredientsHeader.insets = new Insets(0, marginLeft, 3, marginRight);
		gbc_lblIngredientsHeader.gridx = 0;
		gbc_lblIngredientsHeader.gridy = 5;
		add(lblIngredientsHeader, gbc_lblIngredientsHeader);
		
		lblIngredientsList = new JTextArea(txt_lblIngredientsList);
		lblIngredientsList.setFont(new Font("Tahoma", Font.PLAIN, plainTextSize));
		lblIngredientsList.setOpaque(false);
		lblIngredientsList.setEditable(false);
		GridBagConstraints gbc_lblIngredientsList = new GridBagConstraints();
		gbc_lblIngredientsList.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblIngredientsList.fill = GridBagConstraints.VERTICAL;
		gbc_lblIngredientsList.insets = new Insets(0, marginLeft, 10, marginRight);
		gbc_lblIngredientsList.gridx = 0;
		gbc_lblIngredientsList.gridy = 6;
		add(lblIngredientsList, gbc_lblIngredientsList);
		
		/** Zubereitungstext */
		lblPreperationHeader = new JTextArea(txt_lblPreperationHeader);
		lblPreperationHeader.setFont(new Font("Tahoma", Font.BOLD, headerTextSize));
		lblPreperationHeader.setOpaque(false);
		lblPreperationHeader.setEditable(false);
		GridBagConstraints gbc_lblPreperationHeader = new GridBagConstraints();
		gbc_lblPreperationHeader.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblPreperationHeader.insets = new Insets(0, marginLeft, 3, marginRight);
		gbc_lblPreperationHeader.gridx = 0;
		gbc_lblPreperationHeader.gridy = 7;
		add(lblPreperationHeader, gbc_lblPreperationHeader);
		
		txtAreaPreperationText = new JTextArea(txt_lblPreperationText);
		txtAreaPreperationText.setFont(new Font("Tahoma", Font.PLAIN, plainTextSize));
		txtAreaPreperationText.setOpaque(true);
		txtAreaPreperationText.setEditable(false);
		txtAreaPreperationText.setLineWrap(true);
		txtAreaPreperationText.setWrapStyleWord(true);
		JScrollPane textAreaPrepScrollP = new JScrollPane(txtAreaPreperationText);
		textAreaPrepScrollP.setOpaque(false);
		textAreaPrepScrollP.setBorder(null);
		GridBagConstraints gbc_txtAreaPreperationHeader = new GridBagConstraints();
		gbc_txtAreaPreperationHeader.fill = GridBagConstraints.BOTH;
		gbc_txtAreaPreperationHeader.insets = new Insets(0, marginLeft, 10, marginRight);
		gbc_txtAreaPreperationHeader.gridx = 0;
		gbc_txtAreaPreperationHeader.gridy = 8;
		add(textAreaPrepScrollP, gbc_txtAreaPreperationHeader);
		
		/** Vorschaubild */
		if (selRow != -1) {
			lblPicture = null;			
			GridBagConstraints gbc_lblPicture = new GridBagConstraints();
			gbc_lblPicture.anchor = GridBagConstraints.NORTH;
			gbc_lblPicture.insets = new Insets(0, marginLeft, 0, marginRight);
			gbc_lblPicture.gridx = 0;
			gbc_lblPicture.gridy = 1;
			if (!txt_lblPicture.isEmpty()) {
				lblPicture = new JLabel(new ImageIcon(Config.getBildpfad()+txt_lblPicture));
				add(lblPicture, gbc_lblPicture);
			}
		}

		/** Sterne-Bewertung */
		if (selRow != -1) {
			imgStars = new Stars(xmlreader.getRating(recipeID));
			imgStars.setOpaque(false);
			GridBagConstraints gbc_imgStars = new GridBagConstraints();
			gbc_imgStars.insets = new Insets(0, marginLeft, 0, marginRight);
			gbc_imgStars.anchor = GridBagConstraints.NORTH;
			gbc_imgStars.fill = GridBagConstraints.VERTICAL;
			gbc_imgStars.insets = new Insets(10, marginLeft, 5, marginRight);
			gbc_imgStars.gridx = 0;
			gbc_imgStars.gridy = 2;
			add(imgStars, gbc_imgStars);
		}
		
		/** Personenanzahl */
		lblPersAmount = new JTextArea(txt_lblPersAmount);
		lblPersAmount.setFont(new Font("Tahoma", Font.BOLD, plainTextSize));
		lblPersAmount.setOpaque(false);
		lblPersAmount.setEditable(false);
		GridBagConstraints gbc_lblPersAmount = new GridBagConstraints();
		gbc_lblPersAmount.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblPersAmount.insets = new Insets(0, marginLeft, 20, marginRight);
		gbc_lblPersAmount.gridx = 0;
		gbc_lblPersAmount.gridy = 4;
		add(lblPersAmount, gbc_lblPersAmount);
		
		/** Label fuer Kategorie */
		lblCategory = new JTextArea(txt_lblCategories);
		lblCategory.setFont(new Font("Tahoma", Font.BOLD, plainTextSize));
		lblCategory.setOpaque(false);
		lblCategory.setEditable(false);
		GridBagConstraints gbc_lblCategory = new GridBagConstraints();
		gbc_lblCategory.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblCategory.insets = new Insets(0, marginLeft, 10, marginRight);
		gbc_lblCategory.gridx = 0;
		gbc_lblCategory.gridy = 3;
		add(lblCategory, gbc_lblCategory);
	}
}
